//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "FirmSearchDlg.h"
#include "FormUtilities.h"
#include "StringUtilities.h"


//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
//---------------------------------------------------------------------------
__fastcall TfrmFirmSearchDlg::TfrmFirmSearchDlg(TComponent* Owner, TDatabase* dbMain, TGlassesBridge* bridge, TGlassesFirm* selectedFirm)
        : TForm(Owner)
{
   TFormUtilities::LocateForm(( TForm*)Owner, this);

   this->dbMain = dbMain;
   this->bridge = bridge;
   this->selectedFirm = selectedFirm;
   firms = new TStringList();
   firms->Duplicates = dupAccept;
}

void TfrmFirmSearchDlg::RefreshGrid()
{
   int rowCount;
   int i;

   rowCount = 2;
   bridge->GetFirms(firms);

   if ( firms->Count > 1)
      rowCount = firms->Count + 1;

   grdFirms->RowCount = rowCount;

   for ( i = 1; i < grdFirms->RowCount; i++)
      grdFirms->Rows[ i]->Clear();

   for ( i = 0; i < firms->Count; i++)
   {
      TGlassesFirm* firm = ( TGlassesFirm*)firms->Objects[ i];

      grdFirms->Cells[ 0][ i + 1] = firm->name;
   }//for

   TFormUtilities::SetLastColumnWidth( grdFirms);
}


//---------------------------------------------------------------------------

void __fastcall TfrmFirmSearchDlg::FormShow(TObject *Sender)
{
   grdFirms->Cells[ 0][ 0] = "NAME";
   RefreshGrid();
}
//---------------------------------------------------------------------------


//---------------------------------------------------------------------------

void __fastcall TfrmFirmSearchDlg::FormDestroy(TObject *Sender)
{
   TStringUtilites::Clear(firms);
   delete firms;
}
//---------------------------------------------------------------------------


void __fastcall TfrmFirmSearchDlg::btnCloseClick(TObject *Sender)
{
   Close();        
}
//---------------------------------------------------------------------------


void __fastcall TfrmFirmSearchDlg::btnClearClick(TObject *Sender)
{
   edtName->Clear();
}
//---------------------------------------------------------------------------

void __fastcall TfrmFirmSearchDlg::btnFindClick(TObject *Sender)
{
   int index = TStringUtilites::FindBestMatch(firms, edtName->Text);
   if (index >= 0)
   {
      grdFirms->Row = index + 1;
   }
}
//---------------------------------------------------------------------------

void __fastcall TfrmFirmSearchDlg::btnOkClick(TObject *Sender)
{
   TGlassesFirm* temp;

   temp = (TGlassesFirm*)firms->Objects[grdFirms->Row - 1];
   
   selectedFirm->id = temp->id;
   selectedFirm->name = temp->name;
}
//---------------------------------------------------------------------------

void __fastcall TfrmFirmSearchDlg::btnCancelClick(TObject *Sender)
{
   selectedFirm->id = -1;
   selectedFirm->name = "";
}
//---------------------------------------------------------------------------

